package com.chuang.bootplus.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.chuang.bootplus.base.constant.Constant;
import com.chuang.bootplus.base.enumerate.HttpStatusEnum;
import com.chuang.bootplus.base.exception.BusException;
import com.chuang.bootplus.base.utils.ApiResponse;
import com.chuang.bootplus.base.utils.BeanUtil;
import com.chuang.bootplus.entity.RelUserMedal;
import com.chuang.bootplus.entity.SignIn;
import com.chuang.bootplus.mapper.SignInMapper;
import com.chuang.bootplus.po.SignPO;
import com.chuang.bootplus.service.RelUserMedalService;
import com.chuang.bootplus.service.SignInService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chuang.bootplus.vo.SignVO;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ${author}
 * @since 2021-08-17
 */
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class SignInServiceImpl extends ServiceImpl<SignInMapper, SignIn> implements SignInService {

    final RelUserMedalService relUserMedalService;

    @Override
    public ApiResponse<Void> sign(SignPO po) {

        if (po.getUserId() == null) {
            throw new BusException(HttpStatusEnum.RE_CODE_PARAM_ERROR, "参数不能为空！");
        }

        LocalDate now = LocalDate.now();
        SignIn sign = getOne(new LambdaQueryWrapper<SignIn>()
                .eq(SignIn::getUserId, po.getUserId())
                .eq(SignIn::getSignInDate, now));
        if (!(sign != null&&sign.getSignInDate().equals(now))){
            save(new SignIn().setUserId(po.getUserId()).setSignInDate(now));
        }
        int count = count(new LambdaQueryWrapper<SignIn>().eq(SignIn::getUserId, po.getUserId()));

        RelUserMedal one = relUserMedalService.getOne(new LambdaQueryWrapper<RelUserMedal>()
                .eq(RelUserMedal::getMedalId, Constant.MEDAL_TYPE_FIVE)
                .eq(RelUserMedal::getUserId,po.getUserId()));
        if (!(one != null && po.getUserId().equals(one.getUserId())&&one.getMedalId().equals(Constant.MEDAL_TYPE_FIVE))) {
            if (count>=Constant.DAY_NUM7){
                relUserMedalService.save(new RelUserMedal().setUserId(po.getUserId()).setMedalId(Constant.MEDAL_TYPE_FIVE));
            }
        }

        RelUserMedal two = relUserMedalService.getOne(new LambdaQueryWrapper<RelUserMedal>()
                .eq(RelUserMedal::getMedalId, Constant.MEDAL_TYPE_SIX)
                .eq(RelUserMedal::getUserId,po.getUserId()));
        if (!(two != null && po.getUserId().equals(two.getUserId())&&two.getMedalId().equals(Constant.MEDAL_TYPE_SIX))) {
            if (count>=Constant.DAY_NUM15){
                relUserMedalService.save(new RelUserMedal().setUserId(po.getUserId()).setMedalId(Constant.MEDAL_TYPE_SIX));
            }
        }

        RelUserMedal three = relUserMedalService.getOne(new LambdaQueryWrapper<RelUserMedal>()
                .eq(RelUserMedal::getMedalId, Constant.MEDAL_TYPE_SEVEN)
                .eq(RelUserMedal::getUserId,po.getUserId()));
        if (!(three != null && po.getUserId().equals(three.getUserId())&&three.getMedalId().equals(Constant.MEDAL_TYPE_SEVEN))) {
            if (count>=Constant.DAY_NUM30){
                relUserMedalService.save(new RelUserMedal().setUserId(po.getUserId()).setMedalId(Constant.MEDAL_TYPE_SEVEN));
            }
        }

        return new ApiResponse<>();
    }

    @Override
    public ApiResponse<SignVO> signDateList(SignPO po) {
        if (po.getUserId() == null) {
            throw new BusException(HttpStatusEnum.RE_CODE_PARAM_ERROR, "参数不能为空！");
        }

        List<SignIn> list = list(new LambdaQueryWrapper<SignIn>()
                .eq(SignIn::getUserId, po.getUserId())
                .orderByDesc(SignIn::getSignInDate));

        return new ApiResponse<>(BeanUtil.listA2ListB(list,SignVO.class));
    }
}
